package customers;

import avenue.MyWindowEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import model.DBManager;
import model.Vehicle;

public class VehicleDialog extends javax.swing.JFrame {

    public VehicleDialog(Vehicle v) {
        em = DBManager.em;
        if (!em.getTransaction().isActive()){
            em.getTransaction().begin();
        }
        vehicle1 = v;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        categoryBoxRenderer1 = new avenue.CategoryBoxRenderer();
        model.Vehicle vehicle2 = vehicle1;
        programQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT p FROM Program p");
        programList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : programQuery.getResultList();
        brandQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT b FROM Brand b");
        brandList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : brandQuery.getResultList();
        vehicleCategoryQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT v FROM VehicleCategory v");
        vehicleCategoryList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : vehicleCategoryQuery.getResultList();
        vehicleCategoryComboBox = new javax.swing.JComboBox();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        programLabel = new javax.swing.JLabel();
        programComboBox = new javax.swing.JComboBox();
        factoryNameTF = new javax.swing.JTextField();
        regNumberLabel = new javax.swing.JLabel();
        brandLabel = new javax.swing.JLabel();
        vehicleCategoryLabel = new javax.swing.JLabel();
        cardIdTF = new javax.swing.JTextField();
        cardIdLabel = new javax.swing.JLabel();
        colourLabel = new javax.swing.JLabel();
        colourComboBox = new javax.swing.JComboBox();
        factoryNameLabel = new javax.swing.JLabel();
        regNumberTF = new javax.swing.JTextField();
        modelTypeLabel = new javax.swing.JLabel();
        modelTypeComboBox = new javax.swing.JComboBox();
        brandComboBox = new javax.swing.JComboBox();
        barcodeLabel = new javax.swing.JLabel();
        barcodeTF = new javax.swing.JTextField();

        categoryBoxRenderer1.setText("categoryBoxRenderer1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setAlwaysOnTop(true);

        vehicleCategoryComboBox.setRenderer(categoryBoxRenderer1);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleCategoryList, vehicleCategoryComboBox, "vehicleList");
        bindingGroup.addBinding(jComboBoxBinding);
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${categoryId}"), vehicleCategoryComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "categoryId");
        bindingGroup.addBinding(binding);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveButton.setText("Αποθήκευση");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        cancelButton.setText("Ακυρο");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        programLabel.setText("Πρόγραμμα :  ");

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, programList, programComboBox, "programList");
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${cardId.programId}"), programComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${factoryName}"), factoryNameTF, org.jdesktop.beansbinding.BeanProperty.create("text"), "factoryname");
        bindingGroup.addBinding(binding);

        regNumberLabel.setText("Αρ. Πινακίδας:  ");

        brandLabel.setText("Κατασκευαστής: ");

        vehicleCategoryLabel.setText("Κατηγορία :  ");

        cardIdTF.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${cardId.id}"), cardIdTF, org.jdesktop.beansbinding.BeanProperty.create("text"), "arithmosSyskeuis");
        bindingGroup.addBinding(binding);

        cardIdLabel.setText("Αρ. Συσκευής:  ");

        colourLabel.setText("Χρώμα: ");

        colourComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Άσπρο", "Μαύρο", "Μπλε", "Κόκκινο", "Κίτρινο", " " }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${colour}"), colourComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        factoryNameLabel.setText("Εργοστασιακό Όνομα:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${regNumber}"), regNumberTF, org.jdesktop.beansbinding.BeanProperty.create("text"), "regNumber");
        bindingGroup.addBinding(binding);

        modelTypeLabel.setText("Τύπος:");

        modelTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2-DR", "4-DR", "2-DR HBK", "4-DR HBK", "4-DR Wagon" }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${modelType}"), modelTypeComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "modelType");
        bindingGroup.addBinding(binding);

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, brandList, brandComboBox, "brandList");
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${brandId}"), brandComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "brandId");
        bindingGroup.addBinding(binding);

        barcodeLabel.setText("Barcode Συσκευής:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicle2, org.jdesktop.beansbinding.ELProperty.create("${cardId.barcode}"), barcodeTF, org.jdesktop.beansbinding.BeanProperty.create("text"), "barcode");
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(brandLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(brandComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(factoryNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(vehicleCategoryLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(vehicleCategoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(colourLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(colourComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(programLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(programComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cancelButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(saveButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(cardIdLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cardIdTF))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(regNumberLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(regNumberTF, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addComponent(barcodeLabel)))
                        .addGap(9, 9, 9)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(factoryNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(modelTypeLabel)
                        .addGap(18, 18, 18)
                        .addComponent(modelTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(barcodeTF, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regNumberLabel)
                    .addComponent(regNumberTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cardIdLabel)
                    .addComponent(cardIdTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(barcodeLabel)
                    .addComponent(barcodeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vehicleCategoryLabel)
                    .addComponent(vehicleCategoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(programLabel)
                    .addComponent(programComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(brandLabel)
                    .addComponent(factoryNameLabel)
                    .addComponent(factoryNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modelTypeLabel)
                    .addComponent(modelTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(brandComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(colourLabel)
                    .addComponent(colourComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap(77, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        // Έλεγχος των πληροφοριών του νέου οχήματος
        int vehicleCategoryIndex = vehicleCategoryComboBox.getSelectedIndex();
        int programIndex = programComboBox.getSelectedIndex();
        int brandIndex = brandComboBox.getSelectedIndex();
        int colourIndex = colourComboBox.getSelectedIndex();
        int modelTypeIndex = modelTypeComboBox.getSelectedIndex();
        
        if (vehicleCategoryIndex == -1) { 
            JOptionPane.showMessageDialog(this, "Η κατηγορία οχήματος δεν μπορεί να είναι κενή.",
                                          "Σφάλμα",JOptionPane.ERROR_MESSAGE);
            return;
        } else if (programIndex == -1) { 
            JOptionPane.showMessageDialog(this, "Το πρόγραμμα της κάρτας δεν μπορεί να είναι κενό.",
                                          "Σφάλμα",JOptionPane.ERROR_MESSAGE);
            return;
        } else if (brandIndex == -1) { 
            JOptionPane.showMessageDialog(this, "Ο κατασκευαστής του οχήματος δεν μπορεί να είναι κενός.",
                                          "Σφάλμα",JOptionPane.ERROR_MESSAGE);
            return;
        } else if (colourIndex == -1) { 
            JOptionPane.showMessageDialog(this, "Το χρώμα του οχήματος δεν μπορεί να είναι κενό.",
                                          "Σφάλμα",JOptionPane.ERROR_MESSAGE);
            return;
        } else if (modelTypeIndex == -1) { 
            JOptionPane.showMessageDialog(this, "Ο τύπος του μοντέλου δεν μπορεί να είναι κενός.",
                                          "Σφάλμα",JOptionPane.ERROR_MESSAGE);
            return;
        } else if (factoryNameTF.getText().equals("")) { 
            JOptionPane.showMessageDialog(this, "Το εργοστασιακό όνομα δεν μπορεί να είναι κενό.",
                                          "Σφάλμα",JOptionPane.ERROR_MESSAGE);
            return;
        } else if (barcodeTF.getText().equals("")) { 
            JOptionPane.showMessageDialog(this, "Το barcode της κάρτας δεν μπορεί να είναι κενό.",
                                          "Σφάλμα",JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // Διαχείριση παραθύρων
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED,true);
        for (WindowListener l : this.getWindowListeners()) {
            l.windowClosed(we);
        }
        this.setVisible(false); 

    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed

        // Διαχείριση παραθύρων
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED,false);
        for (WindowListener l : this.getWindowListeners()) {
            l.windowClosed(we);
        }
        this.setVisible(false); 

    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel barcodeLabel;
    private javax.swing.JTextField barcodeTF;
    private javax.swing.JComboBox brandComboBox;
    private javax.swing.JLabel brandLabel;
    private java.util.List<model.Brand> brandList;
    private javax.persistence.Query brandQuery;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel cardIdLabel;
    private javax.swing.JTextField cardIdTF;
    private avenue.CategoryBoxRenderer categoryBoxRenderer1;
    private javax.swing.JComboBox colourComboBox;
    private javax.swing.JLabel colourLabel;
    private javax.swing.JLabel factoryNameLabel;
    private javax.swing.JTextField factoryNameTF;
    private javax.swing.JComboBox modelTypeComboBox;
    private javax.swing.JLabel modelTypeLabel;
    private javax.swing.JComboBox programComboBox;
    private javax.swing.JLabel programLabel;
    private java.util.List<model.Program> programList;
    private javax.persistence.Query programQuery;
    private javax.swing.JLabel regNumberLabel;
    private javax.swing.JTextField regNumberTF;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox vehicleCategoryComboBox;
    private javax.swing.JLabel vehicleCategoryLabel;
    private java.util.List<model.VehicleCategory> vehicleCategoryList;
    private javax.persistence.Query vehicleCategoryQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private EntityManager em;
    private Vehicle vehicle1;
    
}
